package com.nit.bike.service.basic.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nit.bike.dto.basic.UserDTO;
import com.nit.bike.dto.utils.PagingDTO;
import com.nit.bike.dto.utils.TableReqDTO;
import com.nit.bike.dto.utils.TableRspDTO;
import com.nit.bike.entity.basic.UserEntity;
import com.nit.bike.mapper.basic.UserMapper;
import com.nit.bike.service.basic.UserService;
import com.nit.bike.utils.MaskUtil;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public TableRspDTO list4Table(TableReqDTO req) throws Exception {
        PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<UserDTO> DTOs = userMapper.list4Table(
                req.getStart(),
                req.getPageSize(),
                req.parseQueryParam("loginName"),
                req.parseQueryParam("userName"),
                "2",
                req.parseQueryParam("userPhone"),
                req.parseQueryParam("userState"),
                req.parseQueryParam("idCard"),
                req.getSortField(),
                req.getSortOrder());
        DTOs.forEach(p -> {
            p.setIdCard(MaskUtil.idCardMask(p.getIdCard()));
            p.setUserPhone(MaskUtil.phoneMask(p.getUserPhone()));
        });
        return new TableRspDTO(new PagingDTO(
                req.getPageNo(),
                req.getPageSize(),
                new PageInfo<>(DTOs).getTotal(),
                DTOs));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(UserDTO userDTO) throws Exception {
        MapperFactory factory = new DefaultMapperFactory.Builder().build();
        UserEntity entity = factory.getMapperFacade().map(userDTO, UserEntity.class);
        Date now = new Date();
        entity.setGmtCreate(now);
        entity.setGmtModify(now);
        return userMapper.insertSelective(entity);
    }

    @Override
    public Integer updateBasicInfo(UserDTO userDTO) throws Exception {
        UserEntity entity = new UserEntity();
        entity.setUserId(userDTO.getUserId());
        entity.setUserName(userDTO.getUserName());
        entity.setUserPhone(userDTO.getUserPhone());
        entity.setIdCard(userDTO.getIdCard());
        return userMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(UserDTO userDTO) throws Exception {
        MapperFactory factory = new DefaultMapperFactory.Builder().build();
        UserEntity entity = factory.getMapperFacade().map(userDTO, UserEntity.class);
        entity.setGmtModify(new Date());
        return userMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateState(String userIds, Integer state) throws Exception {
        String[] arrayUserId = userIds.split(",");
        for (String s : arrayUserId) {
            userMapper.updateState(Long.parseLong(s), state);
        }
        return arrayUserId.length;
    }

    @Override
    public void updateRideId(Long userId, Long rideId) throws Exception {
        userMapper.updateRideId(userId, rideId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer resetPwd(Long userId, String loginPwd) throws Exception {
        return userMapper.resetPwd(userId, loginPwd);
    }

    @Override
    public UserDTO loginCheck(String loginName) throws Exception {
        return userMapper.getByLoginName(loginName);
    }

    @Override
    public UserDTO getByUserId(Long userId) throws Exception {
        return userMapper.getByUserId(userId);
    }

    @Override
    public Integer updateCardId(Long userId, Long cardId) throws Exception {
        return userMapper.updateCardId(userId, cardId);
    }

    @Override
    public UserDTO getByCardId(Long cardId) throws Exception {
        return userMapper.getByCardId(cardId);
    }
}
